package com.zyz.I_多线程.线程安全问题.练习;

/**
 * @author: vick
 * @Date: 2025/3/22
 * @Description:
 * @version: 1.0
 */
public class SafeBankAccountTest {
    public static void main(String[] args) {
        SafeBankAccount safeBankAccount = new SafeBankAccount(1000);

        //存钱线程
        Thread depositThread = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                safeBankAccount.deposit(10);
            }
        });

        //取钱线程
        Thread withDrawThread = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                safeBankAccount.withDraw(10);
            }
        });

        depositThread.start();
        withDrawThread.start();

        try {
            depositThread.join();
            withDrawThread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("最终剩下的钱：" + safeBankAccount.getBalance());
    }
}
